package com.weixing.mall.provider.controller.admin;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.weixing.mall.base.enums.SnCodeEnum;
import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.core.annotation.NoNeedAccess;
import com.weixing.mall.core.annotation.NoRepeatSubmit;
import com.weixing.mall.base.enums.WhetherEnum;
import com.weixing.mall.core.annotation.SysLog;
import com.weixing.mall.core.enums.OperateEnum;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.core.support.auth.SysAuth;
import com.weixing.mall.core.support.auth.SysInfo;
import com.weixing.mall.provider.model.domain.OutDepotBill;
import com.weixing.mall.provider.model.domain.OutDepotBillProduct;
import com.weixing.mall.provider.model.dto.OutDepotBillDto;
import com.weixing.mall.provider.model.dto.OutDepotChgIsFinishDto;
import com.weixing.mall.provider.model.query.DepotBillDetilQuery;
import com.weixing.mall.provider.model.query.OutDepotBillQuery;
import com.weixing.mall.provider.model.vo.OutDepotBillVo;
import com.weixing.mall.provider.service.IOutDepotBillProductService;
import com.weixing.mall.provider.service.IOutDepotBillService;
import com.weixing.mall.provider.support.excalservice.DepotExcalService;
import com.weixing.mall.util.SKUUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @ClassName OutDepotBillController
 * @Description TODO <br/>
 * @Author gjq
 * @Modified By:
 * @Date 2019/10/23
 * @Version 1.0
 **/
@RequestMapping("outDepotBill")
@RestController
@Api(value = "出库单Api",tags = "出库单")
public class OutDepotBillController extends BaseController {

    @Autowired
    private IOutDepotBillService outDepotBillService;

    @Autowired
    private IOutDepotBillProductService outDepotBillProductService;

    @ApiOperation(value = "查询出库单列表")
    @GetMapping("")
    public Result selectOfPage(OutDepotBillQuery outDepotBillQuery){
        if(ObjectUtil.isNull(outDepotBillQuery.getType())){
            outDepotBillQuery.setType(1);
        }
        return ResultUtil.success(outDepotBillService.selectOfPage(initMpPage(),outDepotBillQuery));
    }

    @ApiOperation(value = "新增初始化出库单")
    @GetMapping("/initAdd")
    public Result initAdd(){
        return ResultUtil.success(outDepotBillService.getOutBillSn(SnCodeEnum.OUTBILLSN.getValue()));
    }
    @ApiOperation(value = "根据id查找出库单")
    @GetMapping("getOutDepotBillById/{id}")
    public Result getOutDepotBillById(@PathVariable Long id){
        return ResultUtil.success(outDepotBillService.getOutDepotBillById(id));
    }

    @ApiOperation(value = "库存管理-出库单详情导出Excal")
    @GetMapping("excel/{id}")
    @SysLog(type = OperateEnum.E,desp = "库存管理-出库单详情导出")
    @NoNeedAccess
    public void excel(@PathVariable Long id, HttpServletResponse response){
        OutDepotBillVo odbv = outDepotBillService.getOutDepotBillById(id);
        List<OutDepotBillProduct> list=  outDepotBillProductService.list(new QueryWrapper<OutDepotBillProduct>().eq("out_depot_id",id));
        for(OutDepotBillProduct billProduct:list){
            billProduct.setSize(SKUUtils.size2(billProduct.getSpec()));
            billProduct.setColor(SKUUtils.color2(billProduct.getSpec()));
        }
        DepotExcalService.OutDepotBillProductExcalDownload(odbv.getOutputDepotName(),odbv.getBillSn(),list,"库存出库单导出",response);
    }

    @ApiOperation(value = "出库单详情（根据仓库id和盘点时间）")
    @GetMapping("getOutDepotBillDetilList")
    public Result getOutDepotBillDetilList(DepotBillDetilQuery depotBillDetilQuery){
        return ResultUtil.success(outDepotBillService.getOutDepotBillDetilList(initMpPage(),depotBillDetilQuery));
    }

    @ApiOperation(value = "保存出库单及关联商品信息()")
    @PostMapping("save")
    @NoRepeatSubmit
    public Result save(@SysInfo SysAuth auth, @RequestBody OutDepotBillDto outDepotBillDto){
        outDepotBillService.save(outDepotBillDto,auth.getUid());
        return ResultUtil.success();
    }
    @ApiOperation(value = "审核")
    @PostMapping("saveAndAuit")
    @NoRepeatSubmit
    public Result saveAndAuit(@SysInfo SysAuth auth, @RequestBody OutDepotBillDto outDepotBillDto){
        outDepotBillService.saveAndAuit(outDepotBillDto,auth.getUid());
        return ResultUtil.success();
    }
    /*
    * 去除该方法
    * */
    @Deprecated
    @ApiOperation(value = "修改是否出库状态（先调用库存出库方法进行出库  然后才能调用该方法修改状态）")
    @PostMapping("outDepotChgIsFinish")
    public Result outDepotChgIsFinish(OutDepotChgIsFinishDto outDepotChgIsFinishDto){
       // outDepotBillService.outDepotChgIsFinish(outDepotChgIsFinishDto);
        return ResultUtil.success();
    }

    @ApiOperation(value = "批量删除出库单（只有未出库的才能删，前台判断选中传入都是未出库的id的list）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "arr",value = "出库单id数组",paramType = "path",dataType = "long")
    })
    @PostMapping("delAll/{id}")
    public Result delAll(@PathVariable  long id){
        OutDepotBill bill=outDepotBillService.getById(id);
        if(bill.getIsFinish().equals(WhetherEnum.YES.getValue())){
            return ResultUtil.error("已经审核通过的不允许删除");
        }
        outDepotBillService.removeById(id);
        outDepotBillProductService.remove(new QueryWrapper<OutDepotBillProduct>().eq("out_depot_id",id));
        return ResultUtil.success();
    }
}
